/**************************************************************
  > File Name: timelog.c
  > Author: LuoLiang
  > Mail: 1204553475@qq.com 
  > Created Time: 2021年12月29日 星期三 15时28分20秒
 **************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>

#define BUFSIZE 256

int main(int argc,char **argv)
{
	
	if (argc < 2)
	{
		fprintf(stdout,"Usage:\n");
		exit(1);
	}
	
	FILE *fps;
	time_t tm;
	struct tm *ltime = NULL;
	char buf[BUFSIZE];
	int n = 0;
	fps = fopen(argv[1],"a+");
	if (fps == NULL)
	{
		perror("fopen()");
		exit(1);
	}

	while(fgets(buf,BUFSIZE,fps))
		n++;

	while(1)
	{
		time(&tm);
		ltime = localtime(&tm);
		fprintf(fps,"%-4d%d-%d-%d %d:%d:%d\n",++n,ltime->tm_year+1900,ltime->tm_mon+1,\
					ltime->tm_mday,ltime->tm_hour,ltime->tm_min,ltime->tm_sec);
		fflush(fps);
		sleep(1);
	}
	fclose(fps);
    exit(0);
}
